package com.jy.appmarket.network.engine;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.jy.appmarket.network.resultdata.UserIconResult;

import android.content.Context;

public class UserIconNetEngine extends BaseNetEngine{
	private String mFilePath;
	
	public UserIconNetEngine(String filePath) {
		mResultData = new UserIconResult();
		mHttpMethod = HTTP_POSTBODY;
		mFilePath = filePath;
	}
	
	@Override
	protected String getCommand() {
		return "upload_usericon";
	}
	
	@Override
	protected String getContentType() {
		return "image/jpeg";
	}
	
	@Override
	protected byte[] getBody(Context context) {
		File file = new File(mFilePath);
		InputStream is = null;
		ByteArrayOutputStream baos = null;
		try {
			is = new FileInputStream(file);
			byte[] bytes = new byte[1024 * 100];
			baos = new ByteArrayOutputStream();
			int len = 0;
			while ((len = is.read(bytes)) != -1) {
				baos.write(bytes, 0, len);
			}
			return baos.toByteArray();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
				if (baos != null) {
					baos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return super.getBody(context);
	}

}
